package com.singfee.gisservice.entity;

import lombok.Data;

import java.io.Serializable;
import java.lang.String;

@Data
public class  AjaxResult<T> implements Serializable {
    private boolean success;
    private int code;
    private String msg;
    private T data;
    public AjaxResult()
    {
    }

    public AjaxResult(Type type, String msg, T data) {
        this.code = type.value();
        this.msg = msg;
        if (data!=null) {
            this.data = data;
        }

        if (type.value == Type.SUCCESS.value) {
            this.success = Boolean.TRUE;
        } else {
            this.success = Boolean.FALSE;
        }
    }

    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }

    public static <U> AjaxResult<U> success(U data)
    {
        return AjaxResult.success("操作成功", data);
    }

    public static AjaxResult success(String msg)
    {
        return AjaxResult.success(msg, null);
    }

    public static <U> AjaxResult<U> success(String msg, U data)
    {
        return new AjaxResult(Type.SUCCESS, msg, data);
    }

    public static AjaxResult warn(String msg)
    {
        return AjaxResult.warn(msg, null);
    }

    public static <U> AjaxResult<U> warn(String msg, U data)
    {
        return new AjaxResult(Type.WARN, msg, data);
    }

    public static AjaxResult error()
    {
        return AjaxResult.error("操作失败");
    }

    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    public static <U> AjaxResult<U> error(String msg, U data)
    {
        return new AjaxResult(Type.ERROR, msg, data);
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMsg() {
        return msg;
    }

    public Integer getCode() {
        return code;
    }

    public enum Type
    {
        /** 成功 */
        SUCCESS(200),
        /** 警告 */
        WARN(301),
        /** 错误 */
        ERROR(500);
        private final int value;

        Type(int value)
        {
            this.value = value;
        }

        public int value()
        {
            return this.value;
        }
    }
}
